package com.yunxiangyue


import android.annotation.SuppressLint
import android.database.Cursor
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yunxiangyue.app.R

class com_adapter(var cursor: Cursor):RecyclerView.Adapter<com_adapter.ViewHolder>() {

    var selected="-1"
    inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        val name=view.findViewById<TextView>(R.id.Name)
        val date=view.findViewById<TextView>(R.id.date)
        val comments=view.findViewById<TextView>(R.id.Comments)
        val userPhoto=view.findViewById<ImageView>(R.id.userPhoto)
        val thumbsUp=view.findViewById<Button>(R.id.thumbsUp)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun switchCursor(_cursor: Cursor){
        if(cursor==_cursor) return
        cursor = _cursor
        notifyDataSetChanged()
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(parent.context).inflate(R.layout.comment_item,parent,false)
        view.isLongClickable=true
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        cursor.apply {
            moveToPosition(position)
            holder.also {
                val personId=getColumnIndexOrThrow("id")
                it.name.text=getString(getColumnIndexOrThrow("name"))
                it.date.text=getString(getColumnIndexOrThrow("date"))
                it.userPhoto.setImageResource(getColumnIndexOrThrow("bitmap"))
                Log.d("oo","==================================")
                Log.d("pp",getColumnIndexOrThrow("bitmap").toString())
                it.comments.text=getString(getColumnIndexOrThrow("comment"))
                it.itemView.setOnLongClickListener {
                    selected=personId.toString()
                    false
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return cursor.count
    }


}